/**
 * Copyright [2019] [LiBo/Alex of copyright liboware@gmail.com ]
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.hyts.datasource.aspect;

import com.hyts.datasource.annotation.UseDataSource;
import com.hyts.datasource.context.DataSourceContextHolder;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.annotation.*;
import org.springframework.context.annotation.Configuration;

/**
 * @project-name:coffice-toolbox
 * @package-name:com.hyts.datasource.aspect
 * @author:LiBo/Alex
 * @create-date:2020-05-08 18:04
 * @copyright:libo-alex4java
 * @email:liboware@gmail.com
 * @description:
 */
@Aspect
public class MultiDataSourceAspect {

    public MultiDataSourceAspect(){
        System.out.println("this is init");
    }

    @Pointcut("@within(com.hyts.datasource.annotation.UseDataSource) || " +
            "@annotation(com.hyts.datasource.annotation.UseDataSource)")
    public void pointCut(){

    }

    @Before("pointCut() && @annotation(usingDataSource)")
    public void doBefore(UseDataSource usingDataSource){
        //log.debug("select dataSource---"+usingDataSource.value());
        DataSourceContextHolder.setDatabaseHolder(usingDataSource.value());
    }

    @After("pointCut()")
    public void doAfter(){
        DataSourceContextHolder.clear();
    }


    @AfterThrowing("pointCut()")
    public void doThrow() {
        DataSourceContextHolder.clear();
    }
}
